package com.cjw.controller.livesquare;

import com.cjw.config.annotation.OperationLogAnnotation;
import com.cjw.exceptions.ContextException;
import com.cjw.pojo.dto.livesquare.LiveCommentDTO;
import com.cjw.pojo.entity.contentReview.TextReponse;
import com.cjw.pojo.query.livesquare.AddParentComment;
import com.cjw.pojo.query.livesquare.AddSonLiveComment;
import com.cjw.service.contentReview.TextCensor;
import com.cjw.service.livesquare.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @create_time: 2021.11.28
 * @author : cjw
 * @descripe: 评论controller
 */
@RestController
@RequestMapping("/user/comment")
@Api("生活分享评论资源")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("/addParentComment")
    @ApiOperation("添加父评论")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "添加评论",operDesc = "添加")
    public Boolean addParentComment(@RequestBody @Valid AddParentComment addParentComment) {
        TextReponse textReponse = TextCensor.TextCensor(addParentComment.getContext());
        if (textReponse.getConclusion().equals("合规"))
            return commentService.addParentComment(addParentComment);
        else {
            String msg = textReponse.getData().get(0).getMsg();
            throw new ContextException(msg);
        }
    }

    @PostMapping("/addSonComment")
    @ApiOperation("添加子评论")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "添加子评论",operDesc = "添加")
    public Boolean addSonComment(@RequestBody @Valid AddSonLiveComment comment) {
        TextReponse textReponse = TextCensor.TextCensor(comment.getContext());
        if (textReponse.getConclusion().equals("合规"))
            return commentService.addSonComment(comment);
        else {
            String msg = textReponse.getData().get(0).getMsg();
            throw new ContextException(msg);
        }
    }

    @PostMapping("/deleteComment")
    @ApiOperation("删除评论")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "添加子评论",operDesc = "添加")
    public Boolean deleteComment(Long id, Integer type) {
        if (id == null)
            return false;
        return commentService.deleteComment(id, type);
    }

    @GetMapping("/getAllComment")
    @ApiOperation("获取全部评论")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "获取全部评论",operDesc = "查询")
    public List<List<LiveCommentDTO>> getAllComment(Long id, Integer type) {
        return commentService.getAllComment(id,type);
    }
}
